Ext.define('StaffServer.controller.PostChangeController', {
	extend : 'Ext.app.Controller',

	models: ['PostChangeModel'],
	stores: ['PostChangeStore'],
	views:  [ 
	         'postchange.PostChangeMngr',
	         'postchange.PostChangeSearchPanel',
	         'postchange.PostChangeDetailGrid',
	        
	         'postchange.PostChangeDetailWindow',
	         'postchange.PostChangeDetailForm',
	         'postchange.PostChangeExcelForm',
	         'postchange.PostChangeExcelWindow'
	         ],
	
	tabView: 'StaffServer.view.postchange.PostChangeMngr',
	postChangeDetailWindow:null,
	postChangeDetailForm:null,

	postChangeForm:null,
	postChangeExcelWindow:null,
	postChangeExcelForm:null,
	submitFlag: null,
	retireId: null,
	staffRecord: null,
	refs : [ {
		ref : 'postChangeSearchPanel',
		selector : 'postChangeSearchPanel'
	}, {
		ref : 'postChangeDetailGrid',
		selector : 'postChangeDetailGrid'
	}],

	// 初始化执行语句
	init : function() {                  
		
		postChangeDetailWindow= Ext.widget("postChangeDetailWindow");
		postChangeDetailForm = Ext.ComponentQuery.query('postChangeDetailForm')[0];
		postChangeExcelWindow = Ext.widget("postChangeExcelWindow");
		postChangeExcelForm = Ext.ComponentQuery.query('postChangeExcelForm')[0];
		
		// 控制事件
		this.control({
			'postChangeSearchPanel button[action=search]' : {
				click : this.search
			},
			'postChangeSearchPanel button[action=reset]' : {
				click : this.reset
			},
			
			'postChangeDetailGrid button[action=savePostChange]' : {
				click : this.savePostChange
			},
			'postChangeDetailWindow button[action=close]' : {
				click : this.closeDetailWindow
			},
			'postChangeDetailWindow button[action=submit]' : {
				click : this.submitDetialWindow
			},
			'postChangeDetailGrid button[action=deletePostChange]' : {
				click : this.deletePostChange
			},
			'postChangeDetailGrid button[action=updatePostChange]' : {
				click : this.updatePostChange
			},
			
			
			'postChangeDetailGrid button[action=exportExcel]' : {
				click : this.exportExcel
			},
			'postChangeDetailGrid button[action=importExcel]' : {
				click : this.importExcel
			},
			'postChangeExcelWindow button[action=submit]' : {
				click : this.submitExcelWindow
			},
			'postChangeExcelWindow button[action=close]' : {
				click : this.closeExcelWindow
			}
		});
	},
	
	//关闭窗口
	closeExcelWindow: function(){
		postChangeExcelForm.getForm().reset();
		postChangeExcelWindow.hide();
	},
	
	//提交表单
	submitExcelWindow: function(){
		if (!postChangeExcelForm.form.isValid()) {
            return;
        }
		var $this = this;
		var values = postChangeExcelForm.getValues();
		postChangeExcelForm.getForm().submit({
			waitTitle: '系统提示',   
  			waitMsg: '正在处理,请稍候', 
			clientValidation: true,
		    url: 'postchange/importExcel',
		    params: {
		    	values: values
		    },
		    success: function(form, action) {
		    	Ext.Msg.alert('提示', "操作成功", function(){
		    		$this.closeExcelWindow();
		    		$this.search();
		    	});
		    },
		    failure: function(form, action) {
		    	var resp = Ext.JSON.decode(action.response.responseText);
		    	Ext.Msg.alert('提示', resp.error);
		    }
		});
	},
	
	//导入
	importExcel: function(){
		postChangeExcelWindow.setTitle("导入文件");
		postChangeExcelWindow.show();
	},
	exportExcel: function(){
		var searchPanel = this.getPostChangeSearchPanel();
		var searchParams = searchPanel.getForm().getFieldValues();
		var jsonFilter = Ext.JSON.encode(searchParams);
		window.location.href = 'postchange/exportPostChangeExcel?jsonFilter=' + jsonFilter;
	},
	
	
	
	//更新
	updatePostChange: function(){
		var record = this.getPostChangeDetailGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        postChangeDetailForm.loadRecord(record);
        submitFlag = "update";
        postChangeDetailWindow.setTitle("修改岗位变更信息");
        postChangeDetailWindow.show();
	},
	
	closeDetailWindow:function(){
		postChangeDetailForm.getForm().reset();
		postChangeDetailWindow.hide();
		
	},
	
	
	//删除
	deletePostChange: function(){
		var $this = this;
		var record = this.getPostChangeDetailGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        Ext.MessageBox.confirm('请确认', '确认删除吗?', function(btn, text){
            if (btn == 'yes') {
                showWaitMsg(); 
                Ext.Ajax.request({
                    url: 'postchange/deletePostChange',
                    params: {
                    	sn: record.get("sn")
                    },
                    success: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        if (resp.success) {
                            Ext.Msg.alert('提示', '操作成功', function(){
                            	$this.getPostChangeDetailGrid().getStore().reload();
                            });
                        }
                        else {
                            Ext.Msg.alert('提示', resp.error);
                        }
                    },
                    failure: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        Ext.Msg.alert('提示', resp.error);
                    }
                });
            }
        });
	},
	
	
	//提交表单
	submitDetialWindow: function(){
		if (!postChangeDetailForm.form.isValid()) {
            return;
        }
		var $this = this;
		var values = postChangeDetailForm.getValues();
		postChangeDetailForm.getForm().submit({
			waitTitle: '系统提示',   
  			waitMsg: '正在处理,请稍候', 
			clientValidation: true,
		 url: submitFlag == 'save' ? 'postchange/savePostChange' : 'postchange/updatePostChange',
		//	 url: submitFlag == 'save' ? 'degreeStudy/saveDegreeStudy' : 'degreeStudy/updateDegreeStudy',
			params: {
		    	values: values
		    },
		    success: function(form, action) {
		    	Ext.Msg.alert('提示', "操作成功", function(){
		    		$this.closeDetailWindow();
		    		$this.getPostChangeDetailGrid().getStore().reload();
		    	});
		    },
		    failure: function(form, action) {
		    	var resp = Ext.JSON.decode(action.response.responseText);
		    	Ext.Msg.alert('提示', resp.error);
		    }
		});
	},
	
	//新增
	savePostChange: function(){
		submitFlag = "save";
		postChangeDetailWindow.setTitle("新增岗位变更信息");
		postChangeDetailWindow.show();
		
	},
	
	
	//searchPanel查询
	search : function() {
		var searchPanel = this.getPostChangeSearchPanel();
		var grid = this.getPostChangeDetailGrid();
		grid.store.on('beforeload', function(store){
			var searchParams = searchPanel.getForm().getFieldValues();
			Ext.apply(store.proxy.extraParams, searchParams);
        });
		Ext.ComponentQuery.query('postChangeDetailGrid > pagingtoolbar')[0].moveFirst();
	},
	
	
	//searchPanel重置
	reset: function(){
		var searchPanel = this.getPostChangeSearchPanel();
		searchPanel.getForm().reset();
	}
	

});